package com.xusiyan08.service;

import com.xusiyan08.entity.Favorite;
import com.xusiyan08.repository.FavoriteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class FavoriteService {
    @Autowired
    private FavoriteRepository favoriteRepository;

    public Favorite saveFavorite(Favorite favorite) {
        favorite.setCreatedAt(LocalDateTime.now()); // 设置创建时间
        return favoriteRepository.save(favorite);
    }

    public Optional<Favorite> getFavoriteById(Long favoriteId) {
        return favoriteRepository.findById(favoriteId);
    }

    public List<Favorite> getFavoritesByUserId(Long userId) {
        return favoriteRepository.findByUserId(userId);
    }

    public Optional<Favorite> getFavoriteByUserIdAndItemId(Long userId, Long itemId) {
        return favoriteRepository.findByUserIdAndItemId(userId, itemId);
    }

    public void deleteFavorite(Long favoriteId) {
        favoriteRepository.deleteById(favoriteId);
    }

    public boolean existsByUserIdAndItemId(Long userId, Long itemId) {
        return favoriteRepository.existsByUserIdAndItemId(userId, itemId);
    }
}